package com.devplatform.service.impl.article;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.devplatform.mapper.article.ArticleDtoMapper;
import com.devplatform.mapper.article.UserArticleCollectMapper;
import com.devplatform.mapper.article.UserArticleLikeMapper;
import com.devplatform.pojo.dto.ArticleAttachmentDto;
import com.devplatform.pojo.dto.ArticleDto;
import com.devplatform.pojo.dto.AuthorDto;
import com.devplatform.service.article.IArticleDtoService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.time.LocalDate;
import java.time.Period;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ArticleDtoService implements IArticleDtoService {
    @Resource
    private ArticleDtoMapper articleDtoMapper;

    @Override
    public Page<ArticleDto> pageArticles(Page<ArticleDto> page) {
        return articleDtoMapper.selectArticleList(page);
    }

    @Override
    public Page<ArticleDto> searchArticles(Page<ArticleDto> page, Map<String, Object> params) {
        return articleDtoMapper.searchArticles(page, params);
    }

    @Override
    public Page<ArticleDto> filterArticles(Page<ArticleDto> page, Map<String, Object> params) {
        return articleDtoMapper.filterArticles(page, params);
    }

    @Override
    public Page<ArticleDto> recommendHotArticles(Page<ArticleDto> page, QueryWrapper wrapper) {
        return articleDtoMapper.recommendHotArticles(page, wrapper);
    }

    @Override
    public List<ArticleDto> getHotTop100Articles(String categoryName) {
        // 参数预处理：若传入空字符串，转为null（避免筛选“空板块”）
        String filterCategory = StringUtils.hasText(categoryName) ? categoryName : null;

        // 调用Mapper层，查询指定板块（或全量）的热度Top100文章
        return articleDtoMapper.selectHotTop100Articles(filterCategory);
    }


    @Autowired
    private UserArticleLikeMapper userArticleLikeMapper;

    @Autowired
    private UserArticleCollectMapper userArticleCollectMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ArticleDto getArticleDetails(Long authorId, Long articleId) {
        ArticleDto articleDto = articleDtoMapper.selectArticleDetails(authorId, articleId);
        //判断输入参数是否存在
        if (articleDto == null) {
            return null;
        }
        AuthorDto authorDto = articleDto.getAuthorDto();
        if (authorDto != null) {
            //计算码龄
            LocalDate createTime = authorDto.getCreateTime().toLocalDate();
            //计算日期间隔
            Period period = Period.between(createTime, LocalDate.now());
            authorDto.setCodeAge(period.getYears());
            //统计文章数
            int articleCount = articleDtoMapper.countArticlesByAuthorId(authorDto.getUserId());
            authorDto.setArticleCount(articleCount);
        }
        List<ArticleAttachmentDto> attachments = articleDtoMapper.selectAttachmentsByArticleId(articleId);
        articleDto.setAttachments(attachments);


        return articleDto;
    }
}
